package com.arong.swing.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.arong.swing.util.Tools;

/**
 * 读取properties文件
 * 
 * @author hsy
 * 
 */
public class Config {
	private Properties properties;

	private static Config serviceConfig = null;
	
	public synchronized static Config get(){
		if(serviceConfig == null){
			serviceConfig = new Config("conf/service.conf");
		}
		return serviceConfig;
	}
	
	/**
	 * 初始化Configuration类
	 */
	private Config(String configFile) {
		properties = new Properties();
		InputStream inputFile = null;
		try {
			if(configFile.startsWith("/")){
				inputFile = new FileInputStream(configFile);
			}else{
				inputFile = new FileInputStream(Tools.getAppHome() + configFile);
			}
			
			properties.load(inputFile);
			inputFile.close();
		} catch (FileNotFoundException ex) {
		} catch (IOException ex) {
		}
	}

	/**
	 * 得到key的值
	 * 
	 * @param key
	 *            取得其值的键
	 * @return key的值
	 */
	public String getValue(String key) {
		return properties.getProperty(key);// 得到某一属性的值
	}
	
	/**
	 * get value of int out of configuration
	 * 
	 * @param key
	 *            the key used to look up configuration item
	 * @param defValue
	 *            default value
	 * @return the int value of the configuration, defValue is returned in case
	 *         of the targeted item not found.
	 */
	public int getInt(String key, int defValue) {
		return properties.containsKey(key) ? Integer.parseInt(properties
				.getProperty(key)) : defValue;
	}

	/**
	 * get value of type long out of configuration
	 * 
	 * @param key
	 *            the key used to look up configuration item
	 * @param defValue
	 *            default value
	 * @return the long value of the configuration, defValue is returned in case
	 *         of the targeted item not found.
	 */
	public long getLong(String key, long defValue) {
		return properties.containsKey(key) ? Long.parseLong(properties
				.getProperty(key)) : defValue;
	}

	public boolean getBoolean(String key, boolean defValue) {
		return properties.containsKey(key) ? Boolean.valueOf(properties
				.getProperty(key)) : defValue;
	}

	/**
	 * get value of type String out of configuration
	 * 
	 * @param key
	 *            the key used to look up configuration item
	 * @param defValue
	 *            default value
	 * @return the String value of the configuration, defValue is returned in
	 *         case of the targeted item not found.
	 */
	public String getString(String key, String defValue) {
		return properties.containsKey(key) ? properties.getProperty(key)
				: defValue;
	}

	/**
	 * This will retrieve all configuration items that have @value prefix as its
	 * key's prefix
	 * 
	 * @param prefix
	 * @return Properties
	 */
	public Properties getPropertiesByPrefix(String prefix) {
		Properties props = new Properties();
		for (Object key : properties.keySet()) {
			if (key.toString().startsWith(prefix)) {
				props.put(key, properties.get(key));
			}
		}
		return props;
	}

	/**
	 * Set configuration item on the fly, this can be used to test the system
	 * without a configuration file.
	 * 
	 * @param key
	 *            the key of the item
	 * @param value
	 *            the value of the item
	 */
	public void set(String key, String value) {
		properties.put(key, value);
	}
	public static void main(String[] args) {
		System.out.println(Config.get().getString(Constant.SYS_MYLANG,"zh_CN"));
	}

}
